package chuyentrung.racehorses;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.ViewSwitcher;
import chuyentrung.racehorses.Horse.ColorHorse;

public class ScreenSettingGame extends Activity implements OnClickListener {
	private static int currPlayer = 0;

	private ImageView img, img2, img3, img4;
	private MyTextView index, index2, index3, index4;
	private ViewSwitcher viewSwitcher;

	private boolean isNext = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mode_and_animal);

		viewSwitcher = (ViewSwitcher) findViewById(R.id.viewSwitcher);
		Button mode2 = (Button) findViewById(R.id.pTwo);
		Button mode3 = (Button) findViewById(R.id.pThree);
		Button mode4 = (Button) findViewById(R.id.pFour);
		Button backBtn = (Button) findViewById(R.id.backBtn);

		img = (ImageView) findViewById(R.id.imageAnimal);
		img2 = (ImageView) findViewById(R.id.imageAnimal2);
		img3 = (ImageView) findViewById(R.id.imageAnimal3);
		img4 = (ImageView) findViewById(R.id.imageAnimal4);

		index = (MyTextView) findViewById(R.id.indexPlayer);
		index2 = (MyTextView) findViewById(R.id.indexPlayer2);
		index3 = (MyTextView) findViewById(R.id.indexPlayer3);
		index4 = (MyTextView) findViewById(R.id.indexPlayer4);

		mode2.setOnClickListener(this);
		mode3.setOnClickListener(this);
		mode4.setOnClickListener(this);
		backBtn.setOnClickListener(this);

		img.setOnClickListener(this);
		img2.setOnClickListener(this);
		img3.setOnClickListener(this);
		img4.setOnClickListener(this);
	}

	public void resetSelected() {
		index.setVisibility(View.GONE);
		index2.setVisibility(View.GONE);
		index3.setVisibility(View.GONE);
		index4.setVisibility(View.GONE);
		
		img.setEnabled(true);
		img2.setEnabled(true);
		img3.setEnabled(true);
		img4.setEnabled(true);
		
		currPlayer = 0;
	}
	
	private void check() {
		if (currPlayer == GamePreferences.numPlayers) {
			Toast.makeText(this, "START GAME", Toast.LENGTH_SHORT).show();			
			startActivity(new Intent(ScreenSettingGame.this, RaceHorses.class));			
			finish();		
		}
	}
	
	private void initItems(int numPlayer) {
		GamePreferences.numPlayers = numPlayer;
		viewSwitcher.showNext();
		isNext = !isNext;
		GamePreferences.initPlayers();
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		Assets.playSound(Assets.clickSnd);
		int id = v.getId();

		switch (id) {
		case R.id.pTwo:
			initItems(2);
			break;

		case R.id.pThree:
			initItems(3);
			break;

		case R.id.pFour:
			initItems(4);
			break;

		case R.id.backBtn:
			if (isNext) {
				viewSwitcher.showPrevious();
				isNext = !isNext;
				resetSelected();
			} else
				finish();
			return;

		case R.id.imageAnimal:
			currPlayer++;
			if (currPlayer > GamePreferences.numPlayers)
				break;
			index.setText("P" + currPlayer);
			index.setVisibility(View.VISIBLE);
			img.setEnabled(false);
			GamePreferences.typeHorse[currPlayer - 1] = ColorHorse.RED;
			check();
			break;

		case R.id.imageAnimal2:
			currPlayer++;
			if (currPlayer > GamePreferences.numPlayers)
				break;
			index2.setText("P" + currPlayer);
			index2.setVisibility(View.VISIBLE);
			img2.setEnabled(false);
			GamePreferences.typeHorse[currPlayer - 1] = ColorHorse.BLUE;
			check();
			break;

		case R.id.imageAnimal3:
			currPlayer++;
			if (currPlayer > GamePreferences.numPlayers)
				break;
			index3.setText("P" + currPlayer);
			index3.setVisibility(View.VISIBLE);
			img3.setEnabled(false);
			GamePreferences.typeHorse[currPlayer - 1] = ColorHorse.GREEN;
			check();
			break;

		case R.id.imageAnimal4:
			currPlayer++;
			if (currPlayer > GamePreferences.numPlayers)
				break;
			index4.setText("P" + currPlayer);
			index4.setVisibility(View.VISIBLE);
			img4.setEnabled(false);
			GamePreferences.typeHorse[currPlayer - 1] = ColorHorse.YELLOW;
			check();
			break;

		default:
			break;
		}

	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if (keyCode == KeyEvent.KEYCODE_BACK && isNext) {
			viewSwitcher.showPrevious();
			isNext = !isNext;
			resetSelected();
			return true;
		}

		return super.onKeyDown(keyCode, event);
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		resetSelected();
	}	

}
